#ifndef _TBuffer_H_
#define _TBuffer_H_
#include <map>
#include <mutex>
#include <vector>
namespace tlib
{
	class TBufferPool
	{
	public:
		TBufferPool();
		virtual ~TBufferPool();

		int AllocBlock();
		void FreeBlock(int blockNo);
		struct STBlock* GetBlock(int blockNo);

	private:
		std::mutex mutex_;
		struct STBlock* allBlocks_;
		std::vector<int> noUseBlockNo_;
	};
#define BLOCK_SIZE	8192
#define BLOCK_COUNT	10000
	class TBuffer
	{
	public:
		TBuffer(TBufferPool* pool);
		virtual ~TBuffer();

		bool Read(char* buff, int len, bool del = true);
		bool Write(char* buff, int len);
		int ReadFd(int srcfd, int len);
		int GetReadAbleLen()
		{
			return (writeNo_*BLOCK_SIZE + writeIdx_) - (readNo_*BLOCK_SIZE + readIdx_);
		};
	private:
		std::mutex mutex_;
		int readNo_, writeNo_, readIdx_, writeIdx_;
		TBufferPool* bufferPool_;
		std::vector<int> blocks_;
	};
}
#endif